package com.jzg.common.util;

import ws.schild.jave.AudioAttributes;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.EncodingAttributes;
import ws.schild.jave.InputFormatException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.VideoAttributes;
import ws.schild.jave.VideoSize;

import java.io.File;

public class VideoUtil {

	/**
	 * 转换视频文件为mp4
	 * 
	 * @param file
	 * @return
	 */
	public static void convertToMp4(File source,File target) throws Exception, InputFormatException, EncoderException {
    	AudioAttributes audio = new AudioAttributes();
    	audio.setCodec("aac");
    	audio.setBitRate(new Integer(64000));
    	audio.setChannels(new Integer(2));
    	audio.setSamplingRate(new Integer(22050));
    	VideoAttributes video = new VideoAttributes();
    	video.setCodec("libx264");
    	video.setBitRate(new Integer(160000));
    	video.setFrameRate(new Integer(24));
    	video.setSize(new VideoSize(800, 600));
    	EncodingAttributes attrs = new EncodingAttributes();
    	attrs.setFormat("mp4");
    	attrs.setAudioAttributes(audio);
    	attrs.setVideoAttributes(video);
    	Encoder encoder = new Encoder();
    	encoder.encode(new MultimediaObject(source), target, attrs);
    }

	public static void main(String[] args) throws Exception, EncoderException, Exception {
		String sourcePath="D:\\video\\333.mp4";
		String targetPath="D:\\video\\333-1.mp4";
		File source=new File(sourcePath);
		File target=new File(targetPath);
		convertToMp4(source,target);
	}

}
